package com.ht.persist.example.dao;

import com.google.inject.persist.Transactional;
import com.ht.guice.querydsl.dao.AbstractJPADao;
import com.ht.persist.example.entity.User;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.expr.SimpleExpression;

import javax.inject.Singleton;
import static com.ht.persist.example.entity.QUser.user;


/**
 * Created with IntelliJ IDEA.
 *
 * @author: bcw 14-2-21 下午9:38
 * To change this template use File | Settings | File Templates.
 */
@Singleton
@Transactional
public class UsersDao extends AbstractJPADao<User, Long> {

    public void updateName(String name) {
        update().set(user.name, name);
    }

    public String findName(Long id) {
        return where(user.id.eq(id)).singleResult(user.name);
    }

    @Override
    protected EntityPath<User> getEntityPath() {
        return user;
    }

    @Override
    protected SimpleExpression<Long> getPrimaryKeyExpression() {
        return user.id;
    }

//    @Transactional
//    public Long save(User entity) {
//        if (entity.getId() != null) {
//            update(users).populate(entity).execute();
//            return entity.getId();
//        } else {
//            return insert(users).populate(entity)
//                    .executeWithKey(users.id);
//        }
//    }
}
